package org.asiainfo.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.asiainfo.common.idempotent.annotation.RepeatSubmit;
import org.asiainfo.common.log.annotation.Log;
import org.asiainfo.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.asiainfo.system.service.SysClientService;
import org.asiainfo.system.domain.vo.SysClientVo;
import org.asiainfo.system.domain.bo.SysClientBo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.core.domain.R;
import org.asiainfo.common.web.core.BaseController;
import jakarta.validation.constraints.*;
import org.asiainfo.common.core.validate.AddGroup;
import org.asiainfo.common.core.validate.EditGroup;

import java.util.List;

/**
 * 客户端表
 *
 * @author dotor-ww
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/client")
public class SysClientController extends BaseController {

    private final SysClientService sysClientService;

    /**
     * 分页查询
     *
     * @param sysClientBo 筛选条件
     * @param pageQuery   分页对象
     * @return 查询结果
     */
    @SaCheckPermission("system:client:list")
    @GetMapping("/list")
    public TableDataInfo<SysClientVo> list(SysClientBo sysClientBo, PageQuery pageQuery) {
        return sysClientService.queryPageList(sysClientBo, pageQuery);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param clientId 客户端ID
     * @return 单条数据
     */
    @SaCheckPermission("system:client:query")
    @GetMapping("/{clientId}")
    public R<SysClientVo> getInfo(@NotNull(message = "客户端ID不能为空") @PathVariable("clientId") String clientId) {
        return R.ok(sysClientService.queryById(clientId));
    }

    /**
     * 新增数据
     *
     * @param sysClientBo 实体
     * @return 新增结果
     */
    @SaCheckPermission("system:client:add")
    @Log(title = "客户端配置", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysClientBo sysClientBo) {
        return toAjax(sysClientService.insertByBo(sysClientBo));
    }

    /**
     * 编辑数据
     *
     * @param sysClientBo 实体
     * @return 编辑结果
     */
    @SaCheckPermission("system:client:edit")
    @Log(title = "客户端配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit(interval = 1000)
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysClientBo sysClientBo) {
        return toAjax(sysClientService.updateByBo(sysClientBo));
    }

    /**
     * 删除数据
     *
     * @param clientIds 主键
     * @return 删除是否成功
     */
    @SaCheckPermission("system:client:remove")
    @Log(title = "客户端配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{clientIds}")
    public R<Void> remove(@NotNull(message = "主键不能为空") @PathVariable String[] clientIds) {
        return toAjax(sysClientService.deleteWithValidByIds(List.of(clientIds)));
    }

    /**
     * 修改客户端状态
     *
     * @param sysClientBo 实体
     * @return 编辑结果
     */
    @SaCheckPermission("system:client:edit")
    @Log(title = "客户端配置", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody SysClientBo sysClientBo) {
        return toAjax(sysClientService.updateClientStatus(sysClientBo));
    }
}

